package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartSelectAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartSelectBinding
import com.c.ctgproject.entity.guang.PartSelectEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/9/11
 *描述:配件选择页面 配件列表 页面 废弃
 */
class PartSelectActivity : BaseActivityV2(), PartSelectAdapter.PartSelectAdapterListener {

    private val mBinding by lazy { ActivityPartSelectBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root

    private val mList = ArrayList<PartSelectEntity.DataBean>()
    private val mAdapter by lazy { PartSelectAdapter(this, mList, this) }
    private val mSingleModel by lazy { intent.getBooleanExtra(MyConstants.SINGLE_MODEL, false) }

    private val oldList by lazy { intent.getStringArrayListExtra(MyConstants.DATA) }
    override fun event() {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.getPartMenu(), MyObserverHandlerLoading(this) {
            MyLog.d("配件菜单:$it")
            val data = getGson().fromJson(it, PartSelectEntity::class.java).data
            data?.let { list ->
                list.forEach { bean ->
                    if (bean.name in oldList && bean.name.handlerNull().isNotEmpty()) bean.isSelect = true
                }
                mList.addAll(list)
                mBinding.mRecyclerView.adapter = mAdapter
                mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
            }
        })
    }

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
            val selectList = ArrayList<PartSelectEntity.DataBean>()
            mList.forEach {
                if (it.isSelect) selectList.add(it)
            }
            if (mList.isEmpty()) {
                showToast("请选择配件")
                return@setOnClickListener
            }
            val intent = Intent()
            if (mSingleModel) {
                intent.putExtra(MyConstants.PART_NAME, selectList[0].name.handlerNull())
                intent.putExtra(MyConstants.ABBREVIATION, selectList[0].abbreviation.handlerNull())
            } else {
                val selects = ArrayList<String>()
                selectList.forEach {
                    selects.add(it.name.handlerNull())
                }
                intent.putStringArrayListExtra(MyConstants.DATA, selects)
            }
            setResult(1000, intent)
            finish()
        }
    }

    override fun onItemSelect(position: Int, select: Boolean) {
        if (mSingleModel) {
            mList.forEach {
                it.isSelect = false
            }
            mList[position].isSelect = true
            mAdapter.notifyDataSetChanged()
        } else {
            mList[position].isSelect = select
            mAdapter.notifyItemChanged(position)
        }
    }
}